package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.KaoqinWenjianDistribution;
import com.amsp.oa.oademo.entity.KaoqinWenjianDistributionDetail;
import com.amsp.oa.oademo.mapper.KaoqinWenjianDistributionMapper;
import com.amsp.oa.oademo.mapper.KaoqinWenjianDistributionDetailMapper;
import com.amsp.oa.oademo.service.KaoqinWenjianDistributionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class KaoqinWenjianDistributionServiceImpl extends ServiceImpl<KaoqinWenjianDistributionMapper, KaoqinWenjianDistribution> implements KaoqinWenjianDistributionService {

    @Autowired
    private KaoqinWenjianDistributionDetailMapper detailMapper;

    @Override
    public IPage<KaoqinWenjianDistribution> getPage(Integer current, Integer size, String fileNumber, String fileName) {
        Page<KaoqinWenjianDistribution> page = new Page<>(current, size);
        LambdaQueryWrapper<KaoqinWenjianDistribution> wrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.isNotBlank(fileNumber)) {
            wrapper.like(KaoqinWenjianDistribution::getFileNumber, fileNumber);
        }
        if (StringUtils.isNotBlank(fileName)) {
            wrapper.like(KaoqinWenjianDistribution::getFileName, fileName);
        }
        
        wrapper.orderByDesc(KaoqinWenjianDistribution::getCreateTime);
        return this.page(page, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateWithDetails(KaoqinWenjianDistribution distribution) {
        boolean result = this.saveOrUpdate(distribution);
        
        if (result && distribution.getDetails() != null) {
            // 删除旧的明细记录
            detailMapper.delete(new LambdaQueryWrapper<KaoqinWenjianDistributionDetail>()
                    .eq(KaoqinWenjianDistributionDetail::getDistributionId, distribution.getId()));
            
            // 保存新的明细记录
            for (KaoqinWenjianDistributionDetail detail : distribution.getDetails()) {
                detail.setDistributionId(distribution.getId());
                detailMapper.insert(detail);
            }
        }
        
        return result;
    }

    @Override
    public KaoqinWenjianDistribution getByIdInfo(Long id) {
        KaoqinWenjianDistribution distribution = super.getById(id);
        if (distribution != null) {
            // 查询关联的明细数据
            LambdaQueryWrapper<KaoqinWenjianDistributionDetail> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(KaoqinWenjianDistributionDetail::getDistributionId, id)
                  .eq(KaoqinWenjianDistributionDetail::getIsDeleted, false)
                  .orderByAsc(KaoqinWenjianDistributionDetail::getDistributionNumber);
            
            List<KaoqinWenjianDistributionDetail> details = detailMapper.selectList(wrapper);
            distribution.setDetails(details);
        }
        return distribution;
    }
} 